<script setup>
import { ref, watch } from 'vue'
import { Fold, Expand } from '@element-plus/icons-vue'
import { ArrowDown } from '@element-plus/icons-vue'
import TagsView from '../components/TagsView.vue'
import { useSettingStore } from '../store/setting'
import { useRoute } from 'vue-router'
import router from '../router'
import user from '../api/user'
//获得设置菜单折叠状态store
const settingStore = useSettingStore()
//是否显示输入框
const isShowInput = ref(false)
//输入框输入
const input = ref('')
//获取当前路由
const route = useRoute()
const imgUrl = "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
const isShow = () => {
    isShowInput.value = !isShowInput.value
}
function userLoginOut() {
    localStorage.removeItem('token')
    user.userLoginOut().then((res) => {
        // 登录成功处理
        utils.showMessage(res.data.message, 'success', 2000);
    }).catch((error) => {
        console.log(error);
    })
    router.push('/login')
}

</script>

<template>
    <el-card shadow="hover" class="main-card">
        <!-- 头部 -->
        <template #header>
            <div class="card-header">
                <div class="left-menu">
                    <!-- 折叠按钮 -->
                    <span class="collapse-btn" @click="$emit('collapse')">
                        <el-icon :size="20">
                            <component :is="settingStore.isCollapse ? Expand : Fold" />
                        </el-icon>
                    </span>
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item :to="{ path: '/home' }" style="font-size: large;">首页</el-breadcrumb-item>
                        <el-breadcrumb-item v-if="route.matched[route.matched.length - 1].meta.title != '首页'">
                            {{ route.matched[route.matched.length - 1].meta.parentTitle }}
                        </el-breadcrumb-item>
                        <el-breadcrumb-item v-if="route.matched[route.matched.length - 1].meta.parentTitle">
                            {{ route.matched[route.matched.length - 1].meta.title }}
                        </el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <div class="right-menu">
                    <span @click="isShow">
                        <el-icon :size="20">
                            <Search />
                        </el-icon>
                    </span>
                    <transition name="input-fade" mode="out-in">
                        <el-input class="input" v-if="isShowInput" v-model="input" style="width: 240px"
                            placeholder="Please input" />
                    </transition>
                    <el-dropdown>
                        <span class="el-dropdown-link">
                            <div class="demo-type">
                                <div>
                                    <el-avatar :src="imgUrl" />
                                </div>
                            </div>
                            <el-icon class="el-icon--right">
                                <arrow-down />
                            </el-icon>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item @click="router.push('/PersonalInformation')">个人中心</el-dropdown-item>
                                <el-dropdown-item @click="userLoginOut">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
        </template>
        <!-- 内容 -->
        <el-scrollbar>
            <TagsView />
        </el-scrollbar>
    </el-card>
</template>

<style scoped>
.main-card {
    width: 100%;
}

.card-header {
    /*弹性布局*/
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    font-weight: 600;
}

.left-menu {
    display: flex;
    align-items: center;
    justify-self: center;
    gap: 1rem;
}

.right-menu {
    display: flex;
    align-items: center;
    justify-self: center;
    gap: 1rem;
}



.card-item {
    padding: 8px 0;
    border-bottom: 1px solid var(--el-border-color-light);
}

/* 按钮悬停效果 */
.collapse-btn {
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
        color: var(--el-color-primary);
    }
}

/* 淡入淡出效果 */
.input-fade-enter-active,
.input-fade-leave-active {
    transition: all 0.3s ease;
}

.input-fade-enter-from,
.input-fade-leave-to {
    opacity: 0;
    transform: translateY(-10px);
}

/* 滑动效果 */
.slide-enter-active {
    transition: all 0.3s ease-out;
}

.slide-leave-active {
    transition: all 0.3s ease-in;
}

.slide-enter-from {
    opacity: 0;
    transform: translateX(-20px);
}

.slide-leave-to {
    opacity: 0;
    transform: translateX(20px);
}

.demo-type {
    display: flex;
}

.el-dropdown-link {
    display: flex;
    justify-content: center;
    align-items: center;
}

.demo-type>div {
    flex: 1;
    text-align: center;
}

.demo-type>div:not(:last-child) {
    border-right: 1px solid var(--el-border-color);
}

.example-showcase .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
}

/* 移除所有元素的焦点轮廓 */
.el-dropdown-link:focus {
    outline: none;
}

@media (max-width: 768px) {
    .card-header {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 4px;
        overflow-x: auto;
        white-space: nowrap;
    }

    .left-menu {
        flex: 1 1 auto;
        min-width: 0;
        gap: 4px;
        overflow-x: auto;
        white-space: nowrap;
        display: flex;
        align-items: center;
    }

    .el-breadcrumb {
        font-size: 12px !important;
        white-space: nowrap;
        overflow-x: auto;
        max-width: 100vw;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .el-breadcrumb__item {
        display: inline-block;
        max-width: 90vw;
        text-overflow: ellipsis;
        overflow: hidden;
        vertical-align: top;
    }

    .right-menu {
        flex: 0 0 auto;
        margin-left: 8px;
        white-space: nowrap;
        display: flex;
        align-items: center;
    }
}
</style>